package com.example.test;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.example.test.demin.TingcheChang;
import com.example.test.demin.User;
import com.example.test.utils.DbDaoUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

public class CarNumberPayListActivity extends BaseActivity implements OnClickListener {

	@ViewInject(R.id.txt_title)
	private TextView txt_title;
	@ViewInject(R.id.imgbtn_left)
	private ImageView imgbtn_left;
	@ViewInject(R.id.rl_tiltebar_left)
	private  RelativeLayout rl_left;
	@ViewInject(R.id.rl_tiltebar_right)
	private RelativeLayout rl_right;
	@ViewInject(R.id.imgbtn_right)
	private ImageView imagebtn_right;
	private String tingchechang;
	private String carnumber;
	private TingcheChang tingcheinfo;
	@ViewInject(R.id.ly_ensure_list)
	private LinearLayout ly_ensure_list;
	@ViewInject(R.id.rl_notfind)
	private RelativeLayout rl_notfind;
	@ViewInject(R.id.tv_ting_name)
	private TextView tv_ting_name;
	@ViewInject(R.id.tv_ting_idname)
	private TextView tv_ting_idname;
	@ViewInject(R.id.tv_ting_tnumber)
	private TextView tv_ting_tnumber;
	@ViewInject(R.id.tv_ting_carnumber)
	private TextView tv_ting_carnumber;
	@ViewInject(R.id.btn_ting_pay)
	private TextView btn_ting_pay;
	@ViewInject(R.id.tv_ting_cost)
	private TextView tv_ting_cost;
	private DbDaoUtils dao;
	private SharedPreferences sp;
	private String username;
	private User user;
	private double cost;
	private double yu;
	private long ids;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mTintManager.setStatusBarTintEnabled(true);
		mTintManager.setStatusBarTintResource(R.color.darkwhite);
		setContentView(R.layout.activity_carnmber_paylsit);
		ViewUtils.inject(this);
		txt_title.setText(R.string.carnumberpay);
		imgbtn_left.setBackgroundResource(R.drawable.title_back);
		imagebtn_right.setVisibility(View.INVISIBLE);
		rl_left.setOnClickListener(this);
		Intent intent = getIntent();
		tingchechang = intent.getStringExtra("tingchechang");
		carnumber = intent.getStringExtra("carnumber");
		dao = new DbDaoUtils(this);
		tingcheinfo = dao.findTingcheInfobyCarnumber(tingchechang,carnumber);
		if(tingcheinfo==null){
			rl_notfind.setVisibility(View.VISIBLE);
			ly_ensure_list.setVisibility(View.GONE);
		}else{
			tv_ting_name.setText(tingcheinfo.getName());
			tv_ting_idname.setText(tingcheinfo.getIdname());
			tv_ting_cost.setText(String.valueOf(tingcheinfo.getMoney()));
			tv_ting_carnumber.setText(tingcheinfo.getCarnumber());
			tv_ting_tnumber.setText(tingcheinfo.getTnumber());
		}
		btn_ting_pay.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.rl_tiltebar_left:
			this.finish();
			break;
		case R.id.btn_ting_pay:
			sp = getSharedPreferences("user", MODE_PRIVATE);
			username = sp.getString("username", "");
			if(TextUtils.isEmpty(username)){
				Toast.makeText(getApplicationContext(), "登陆已过期，请重新登陆", 0).show();
			}else {
				user = dao.findUserInfo(username);
				cost = tingcheinfo.getMoney();
				yu=user.getMoney()-cost;
				if(yu>=0){
					Toast.makeText(getApplicationContext(), R.string.paysuscess, Toast.LENGTH_SHORT).show();
					dao.updateMoney(username, yu);
					ids = dao.insertBill(tingcheinfo.getName(), tingcheinfo.getCarnumber(), tingcheinfo.getMoney());
					Intent intent=new Intent(this, PayBilldetailActivity.class);
					intent.putExtra("fromwhat", "pay");
					int ii = (int)ids;  
					intent.putExtra("ids", ii);
					startActivity(intent);
					this.finish();										
					ActivityControler.finishAll();
				}else{
					Toast.makeText(getApplicationContext(), R.string.payfail, Toast.LENGTH_SHORT).show();
				}
			}
			
			break;

		default:
			break;
		}
	}
}
